// Copyright 2012 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
	"walk"
	. "walk/declarative"
)

func main() {

	//main
	mw := &MyMainWindow{}

	if err := (MainWindow{
		Icon:     "../img/stop.ico",
		AssignTo: &mw.MainWindow,
		Title:    "BolgSearch -- by tim_zhang",

		MenuItems: []MenuItem{
			Menu{
				Text: "&编辑",
				Items: []MenuItem{
					Separator{},
					Action{
						Text:        "退出",
						OnTriggered: func() { mw.Close() },
					},
				},
			},
			Menu{
				Text: "&收藏",
				Items: []MenuItem{
					Separator{},
					Action{
						Text:        "查看",
				//		OnTriggered: mw.search,
					},
				},
			},
			Menu{
				Text: "&帮助",
				Items: []MenuItem{
					Action{
						Text:        "关于",
				//		OnTriggered: mw.aboutAction_Triggered,
					},
				},
			},
		},
		MinSize: Size{1000, 600},
		Layout:  VBox{MarginsZero: true},

		Children: []Widget{
			Composite{

				MaxSize: Size{0, 50},
				Layout:  HBox{},
				Children: []Widget{
					PushButton{
						AssignTo: &mw.favorites,
						Text:     "收藏",
					},
					PushButton{
						AssignTo: &mw.unfavorites,
						Text:     "取消收藏",
					},
					Label{Text: "网站: "},
					RadioButtonGroup{
						DataMember: "Web",
						Buttons: []RadioButton{
							RadioButton{
								Name:     "all",
								Text:     "全部",
								Value:    "1",
								AssignTo: &mw.all,
							},
							RadioButton{
								Name:     "jianshu",
								Text:     "简书",
								Value:    "2",
								AssignTo: &mw.jianshu,
							},
							RadioButton{
								Name:     "juejin",
								Text:     "掘金",
								Value:    "3",
								AssignTo: &mw.juejin,
							},
							RadioButton{
								Name:     "bokeyuan",
								Text:     "博客园",
								Value:    "4",
								AssignTo: &mw.bokeyuan,
							},
							RadioButton{
								Name:     "csdn",
								Text:     "CSDN",
								Value:    "5",
								AssignTo: &mw.csdn,
							},
							RadioButton{
								Name:     "oschina",
								Text:     "OSCHINA",
								Value:    "6",
								AssignTo: &mw.os,
							},
						},
					},
					Label{Text: "关键词: "},
					LineEdit{
						AssignTo: &mw.keywords,
						Text:     "golang",
						MinSize:Size{Width:50,Height:0},
					},
					PushButton{
						AssignTo: &mw.query,
						Text:     "查询",
					},
					PushButton{
						AssignTo: &mw.nextquery,
						Text:     "下一页",
					},
					PushButton{
						AssignTo: &mw.prvquery,
						Text:     "上一页",
					},
				},
			},

			Composite{
				Layout: Grid{Columns: 2, Spacing: 10},
				Children: []Widget{
					ListBox{
						MaxSize:               Size{200, 0},
						AssignTo:              &mw.lb,
				//		OnCurrentIndexChanged: mw.lb_CurrentIndexChanged,
				//		OnItemActivated:       mw.lb_ItemActivated,
					},

					WebView{
						//MinSize:  Size{1000, 0},
						AssignTo: &mw.wv,
					},
				},
			},
		},
	}.Create()); err != nil {
		log.Fatal(err)
	}

	mw.query.Clicked().Attach(func() {
		go func() {

	//		mw.GetList(1)

		}()
	})

	mw.nextquery.Clicked().Attach(func() {
		go func() {



		}()
	})

	mw.prvquery.Clicked().Attach(func() {
		go func() {
			mw.page = mw.page - 1
			if mw.page == 0 {
				mw.page = 1
			}

			//mw.GetList(mw.page)

		}()
	})

	mw.favorites.Clicked().Attach(func() {
		go func() {
		//	mw.addFavorite()
			mw.favorites.SetEnabled(false)
			mw.unfavorites.SetEnabled(true)
			walk.MsgBox(mw, "提示", "收藏成功", walk.MsgBoxIconInformation)
		}()
	})

	mw.unfavorites.Clicked().Attach(func() {
		go func() {
		//	mw.delFavorite()
			mw.favorites.SetEnabled(true)
			mw.unfavorites.SetEnabled(false)
			walk.MsgBox(mw, "提示", "取消收藏", walk.MsgBoxIconWarning)

		}()
	})

	//初始化
	//mw.wv.SetURL("file:///" + getCurrentDirectory() + "/data/init.html")
	mw.all.SetChecked(true)
	mw.favorites.SetEnabled(false)
	mw.unfavorites.SetEnabled(false)
	mw.curtitle = ""
	mw.cururl = ""
	mw.Run()
}

type MyMainWindow struct {
	*walk.MainWindow
	lb          *walk.ListBox
	te          *walk.TextEdit
	wv          *walk.WebView
	all         *walk.RadioButton
	jianshu     *walk.RadioButton
	juejin      *walk.RadioButton
	bokeyuan    *walk.RadioButton
	csdn        *walk.RadioButton
	os          *walk.RadioButton
	keywords    *walk.LineEdit
	model       *Model
	query       *walk.PushButton
	nextquery   *walk.PushButton
	prvquery    *walk.PushButton
	favorites   *walk.PushButton
	unfavorites *walk.PushButton
	page        int
	curtitle    string
	cururl      string
}
type Model struct {
	walk.ListModelBase
	items []Item
}
type Item struct {
	name  string
	value string
}